package com.github.catvod.spider;

import android.text.TextUtils;

import com.github.catvod.bean.Result;
import com.github.catvod.bean.Vod;
import com.github.catvod.crawler.Spider;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Json;
import com.github.catvod.utils.Util;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author fs
 * 万能影视搜索接口仅搜索
 */
public class VideoSearch extends Spider {
    private String API="https://api.milorapart.top/api/searchvedio";


    // 获取Headers
    private HashMap<String, String> headers() {
        HashMap<String, String> headers = new HashMap<>();
        headers.put("User-Agent", Util.CHROME);
        return headers;
    }

    @Override
    public String detailContent(List<String> ids) throws Exception {
        String url = ids.get(0);
        String[] split = url.split("&");
        System.out.println(split[0]);
        System.out.println(split[1]);
        String id = split[0];
        String name = split[1];
        HashMap<String, String> params = new HashMap<>();
        params.put("name", name);
        params.put("id", id);
        JSONObject jsonObject = Json.parseJson(OkHttp.string(API, params, headers()));
        ArrayList<String> playerList = new ArrayList<>();
        if (jsonObject.getString("code").equals("200")){
            System.out.println(jsonObject);
            JSONArray data = jsonObject.getJSONArray("data");
            String title = jsonObject.getString("name");
            String pic = jsonObject.getString("pic");
            String desc = jsonObject.getString("introduction");
            for (int i = 0; i < data.length(); i++) {
                JSONObject vod = data.getJSONObject(i);
                String playUrl = vod.getString("url");
                String playName = vod.getString("num");
                playerList.add(playName + "$" + playUrl);
            }
            Vod vod = new Vod();
            vod.setVodId(id);
            vod.setVodName(title);
            vod.setVodPic(pic);
            vod.setVodContent(desc);
            vod.setVodRemarks("");
            vod.setVodPlayFrom("m3u8");
            vod.setVodPlayUrl(TextUtils.join("#", playerList));
            return Result.string(vod);
        }
        return "";
    }

    @Override
    public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
        return Result.get().url(id).header(headers()).string();
    }

    @Override
    public String searchContent(String key, boolean quick) throws Exception {
        HashMap<String, String> params = new HashMap<>();
        params.put("name", key);
        JSONObject jsonObject = Json.parseJson(OkHttp.string(API, params, headers()));
        if (jsonObject.getString("code").equals("200")){
            JSONArray data = jsonObject.getJSONArray("data");
            List<Vod> list=new ArrayList<>();
            for (int i = 0; i <data.length(); i++) {
                JSONObject vod = data.getJSONObject(i);
                String id=vod.getString("id")+"&"+key;
                list.add(new Vod(id, vod.getString("name"), vod.getString("name")));
            }
            return Result.string(list);

        }

        return super.searchContent(key, quick);
    }
}
